package ms.platform.util;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ImportExcelUntil {

    public static HSSFSheet importExcelis2003(MultipartFile file) throws Exception {
        //装载流
        POIFSFileSystem fs = new POIFSFileSystem(file.getInputStream());
        HSSFWorkbook hw = new HSSFWorkbook(fs);
        HSSFSheet sheet = hw.getSheetAt(0);
        return sheet;
    }

    public static XSSFSheet importExcelis2007(MultipartFile file) throws Exception {
        //装载流
        XSSFWorkbook xwb = new XSSFWorkbook(file.getInputStream());
        XSSFSheet sheet = xwb.getSheetAt(0);
        return sheet;
    }

    /**
     * 处理val
     * @return
     */
    public static String getVal(HSSFCell hssfCell) {
        if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            return hssfCell.getStringCellValue();
        } else {
            if (StringUtil.isNullOrBlock(hssfCell)) {
                return null;
            }
            return String.valueOf(hssfCell.getNumericCellValue());
        }
    }

    /**
     * 处理val
     *
     * @return
     */
    public static String getValx(XSSFCell xssfCell) {
        if (xssfCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            return xssfCell.getStringCellValue();
        } else {
            if (StringUtil.isNullOrBlock(xssfCell.getRawValue())) {
                return null;
            }
            return String.valueOf(xssfCell.getNumericCellValue());
        }
    }

    public static boolean isExcel2003(String filePath) {
        return filePath.matches("^.+\\.(?i)(xls)$");
    }

    /**
     * @描述：是否是2007的excel，返回true是2007
     */
    public static boolean isExcel2007(String filePath) {
        return filePath.matches("^.+\\.(?i)(xlsx)$");
    }

}
